package com.workManager.controller;

import com.github.pagehelper.PageInfo;

import com.wordnik.swagger.annotations.ApiOperation;
import com.workManager.base.BaseController;

import com.workManager.config.RespCode;
import com.workManager.pojo.TbProjectTasktime;


import com.workManager.pojo.TdAuthority;
import com.workManager.service.AuthorityService;
import com.workManager.service.ProjectService;
import com.workManager.service.ProjectTasktimeService;
import com.workManager.service.UserService;
import com.workManager.vo.CountProjectVo;
import com.workManager.vo.ProjectDetailVo;

import com.workManager.vo.TaskTimeTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.*;

/**
 * Created by Thinkpad on 2017/1/18 0018.
 */
@Controller
@RequestMapping("/projectTasktime")
public class ProjectTasktimeController extends BaseController{

    @Autowired
    private ProjectTasktimeService projectTasktimeService;

    @Autowired
    private ProjectService projectService;

    @Autowired
    private UserService userService;


    @Autowired
    private AuthorityService authorityService;


    @ApiOperation(value="查询项目人天详情",notes = "查询项目人天详情")
    @RequestMapping(value="/queryProjectDetails",method = RequestMethod.POST)
    public  Object queryProjectDetails(HttpServletRequest request,
                                       @RequestParam(value = "pageNumber",required = false) String pageNumber,
                                       @RequestParam(value="pageSize", required = false) String pageSize,
                                       @RequestParam(value = "startTime", required =  false) String startTime,
                                       @RequestParam(value = "endTime", required =  false) String endTime,
                                       @RequestParam(value = "projectCode", required = false) String projectCode,
                                       @RequestParam(value = "projectName", required = false) String projectName,
                                       @RequestParam(value = "employName", required = false) String employName,
                                       @RequestParam(value = "loginCode", required = false) String loginCode){

        ModelMap modelMap = new ModelMap();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);
        Map<String,Object> queryParam = new HashMap<String, Object>();
        queryParam.put("pagenum",pagenum);
        queryParam.put("pagesize",pagesize);
        queryParam.put("startTime",startTime);
        queryParam.put("endTime",endTime);
        queryParam.put("projectCode",projectCode);
        queryParam.put("projectName",projectName);
        queryParam.put("userName",employName);
        queryParam.put("loginCode",loginCode);

        PageInfo<ProjectDetailVo> projectDetailVoPageInfo = projectTasktimeService.queryProjectDetails(queryParam);
        Map<String,Object> dataMap = new HashMap<String, Object>();
        dataMap.put("total", projectDetailVoPageInfo.getTotal());
        dataMap.put("rows", projectDetailVoPageInfo.getList());
        return  setSuccessModelMap(modelMap,dataMap);
    }


    @ApiOperation(value="人天Excel文件上传接口",notes="人天Excel文件上传接口")
    @RequestMapping(value="/upLoadExcel",method = RequestMethod.POST)
    public Object upLoadExcel(@RequestParam()MultipartFile file){

        if(file==null){
            return  null;
        }

        ModelMap modelMap = new ModelMap();
        String name = file.getOriginalFilename();
        long size = file.getSize();

        if(name==null||"".equals(name)&&size==0){
            return  null;
        }

        String msg = "上传成功";
        try {
            projectTasktimeService.uploadExcel(name,file);
        } catch (Exception e) {
            e.printStackTrace();
            msg = e.getMessage();
            return setModelMap(modelMap,RespCode.INTERNAL_SERVER_ERROR,msg);
        }
        return  setModelMap(modelMap, RespCode.OK,msg);

    }

    @ApiOperation(value ="项目汇总",notes = "项目汇总")
    @RequestMapping(value="/countByProject",method = RequestMethod.POST)
    public Object countByProject(HttpServletRequest request,
                                 @RequestParam(value = "pageNumber",required = false) String pageNumber,
                                 @RequestParam(value="pageSize", required = false) String pageSize,
                                 @RequestParam(value = "startTime", required =  false) String startTime,
                                 @RequestParam(value = "endTime", required =  false) String endTime,
                                 @RequestParam(value = "projectCode", required = false) String projectCode,
                                 @RequestParam(value = "projectName", required = false) String projectName,
                                 @RequestParam(value = "employName", required = false) String employName,
                                 @RequestParam(value = "loginCode", required = false) String loginCode){

        ModelMap modelMap = new ModelMap();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);
        Map<String,Object> dataMap = new HashMap<String, Object>();
        Map<String,Object> map = new HashMap();
        map.put("pagenum",pagenum);
        map.put("pagesize",pagesize);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("projectCode",projectCode);
        map.put("projectName",projectName);
        map.put("loginCode",loginCode);

        PageInfo<CountProjectVo> projectVoList =  projectTasktimeService.countByProject(map);

        dataMap.put("rows",projectVoList.getList());
        dataMap.put("total",projectVoList.getTotal());

        return  setSuccessModelMap(modelMap,dataMap);

    }

    @ApiOperation(value ="人员汇总",notes = "人员汇总")
    @RequestMapping(value="/countByStaff",method = RequestMethod.POST)
    public Object countByStaff(HttpServletRequest request,
                                 @RequestParam(value = "pageNumber",required = false) String pageNumber,
                                 @RequestParam(value="pageSize", required = false) String pageSize,
                                 @RequestParam(value = "startTime", required =  false) String startTime,
                                 @RequestParam(value = "endTime", required =  false) String endTime,
                                 @RequestParam(value = "projectCode", required = false) String projectCode,
                                 @RequestParam(value = "projectName", required = false) String projectName,
                                 @RequestParam(value = "employName", required = false) String employName,
                                @RequestParam(value = "loginCode", required = false) String loginCode){
        ModelMap modelMap = new ModelMap();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);
        Map<String,Object> dataMap = new HashMap<String, Object>();
        Map<String,Object> map = new HashMap();
        map.put("pagenum",pagenum);
        map.put("pagesize",pagesize);
        map.put("startTime",startTime);
        map.put("endTime",endTime);
        map.put("projectCode",projectCode);
        map.put("projectName",projectName);
        map.put("employName",employName);
        map.put("loginCode",loginCode);

        List<TdAuthority> authorityList = authorityService.query(map);
        PageInfo<CountProjectVo> projectVoList ;
        //超级权限人员,返回全部数据
        if(authorityList.size()>0){
            projectVoList =  projectTasktimeService.countByStaffAll(map);
        }else{//普通人员，返回对应数据
            projectVoList =  projectTasktimeService.countByStaff(map);
        }


        dataMap.put("rows",projectVoList.getList());
        dataMap.put("total",projectVoList.getTotal());
        return  setSuccessModelMap(modelMap,dataMap);
    }

    @ApiOperation(value ="人天变化趋势",notes = "项目人天统计")
    @RequestMapping(value="/taskTimeChange",method = RequestMethod.POST)
    public Object taskTimeChange(HttpServletRequest request,
                                 @RequestParam(value = "startTime", required =  false) String startTime,
                                 @RequestParam(value = "endTime", required =  false) String endTime,
                                 @RequestParam(value = "projectId",required = false) String projectId){
        ModelMap modelMap = new ModelMap();
        Map<String,Object> map = new HashMap();
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        map.put("projectId", projectId);

        List<TbProjectTasktime> tasktimeList = projectTasktimeService.taskTimeChange(map);

        return  setSuccessModelMap(modelMap,tasktimeList);
    }

    @ApiOperation(value = "人天类型分布",notes = "项目人天统计")
    @RequestMapping(value = "/taskTimeTypeSpread",method = RequestMethod.POST)
    public Object taskTimeTypeSpread(
            @RequestParam(value = "startTime", required =  false) String startTime,
            @RequestParam(value = "endTime", required =  false) String endTime,
            @RequestParam(value = "projectId",required = false) String projectId){
        ModelMap modelMap = new ModelMap();
        Map<String,Object> queryMap = new HashMap();
        queryMap.put("startTime", startTime);
        queryMap.put("endTime", endTime);
        queryMap.put("projectId", projectId);
        List<TaskTimeTypeVo> taskTimeTypeVos = projectTasktimeService.taskTimeTypeChange(queryMap);
        List<Map<String,Object>> dataMap = new ArrayList<Map<String, Object>>();
        BigDecimal total = new BigDecimal(0);//总的人天
        MathContext mc = new MathContext(4, RoundingMode.HALF_DOWN);

        Set s = new HashSet();
        for(int i=0;i<taskTimeTypeVos.size();i++){
            s.add(taskTimeTypeVos.get(i).getTaskTimeTypeName());
        }

        for(TaskTimeTypeVo vo:taskTimeTypeVos){
            total = total.add(vo.getTaskTimeTotal());
        }

        Iterator<String> t = s.iterator();
        while (t.hasNext()){
            String st = t.next().toString();
            Map<String,Object> map = new HashMap<String, Object>();
            List<TaskTimeTypeVo> voLis = new ArrayList<TaskTimeTypeVo>();
            BigDecimal totalPer = new BigDecimal(0);//每一阶段的人天
            for(TaskTimeTypeVo vo:taskTimeTypeVos){
                  if(st.equals(vo.getTaskTimeTypeName())){
                     vo.setTaskRatio(vo.getTaskTimeTotal().divide(total,mc));
                     voLis.add(vo);
                      totalPer = totalPer.add(vo.getTaskTimeTotal());
                  }
            }
            map.put("name",st);
            map.put("data",voLis);
            map.put("totalTaskTime",totalPer);
            dataMap.add(map);
        }
        return  setSuccessModelMap(modelMap,dataMap);
    }

    @ApiOperation(value = "员工人天分布",notes = "项目人天统计")
    @RequestMapping(value = "/taskTimeStaffSpread",method = RequestMethod.POST)
    public Object taskTimeStaffSpread(
            @RequestParam(value = "startTime", required =  false) String startTime,
            @RequestParam(value = "endTime", required =  false) String endTime,
            @RequestParam(value = "projectId",required = false) String projectId){

        ModelMap modelMap = new ModelMap();
        Map<String,Object> queryMap = new HashMap();
        queryMap.put("startTime", startTime);
        queryMap.put("endTime", endTime);
        queryMap.put("projectId", projectId);
        List<TaskTimeTypeVo> taskTimeTypeVos = projectTasktimeService.taskTimeStaffSpread(queryMap);
        List<Map<String,Object>> dataMap = new ArrayList<Map<String, Object>>();
        BigDecimal total = new BigDecimal(0);
        Set s = new HashSet();
        for(TaskTimeTypeVo vo:taskTimeTypeVos){
            s.add(vo.getUserName());
            total = total.add(vo.getTaskTimeTotal());
        }
        Iterator<String> is = s.iterator();
        while(is.hasNext()){
            String userName = is.next();
            Map map = new HashMap();
            BigDecimal totalPer = new BigDecimal(0);
            MathContext mc = new MathContext(4, RoundingMode.HALF_DOWN);
            List<TaskTimeTypeVo> list = new ArrayList<TaskTimeTypeVo>();
            for(TaskTimeTypeVo vo:taskTimeTypeVos){
                if(userName.equals(vo.getUserName())){
                    vo.setTaskRatio(vo.getTaskTimeTotal().divide(total,mc));
                    list.add(vo);
                    totalPer = totalPer.add(vo.getTaskTimeTotal());
                }
            }
            map.put("name",userName);
            map.put("data",list);
            map.put("totalTaskTime",totalPer);
            dataMap.add(map);
            Collections.sort(dataMap, new Comparator<Map<String, Object>>() {
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    return((BigDecimal) o2.get("totalTaskTime")).compareTo((BigDecimal) o1.get("totalTaskTime"));
                }
            });
        }
        return  setSuccessModelMap(modelMap,dataMap);
    }

    @ApiOperation(value ="人天变化趋势",notes = "员工人天统计")
    @RequestMapping(value="/taskTimeStaffChange",method = RequestMethod.POST)
    public Object taskTimeStaffChange(
            @RequestParam(value = "startTime", required =  false) String startTime,
            @RequestParam(value = "endTime", required =  false) String endTime,
            @RequestParam(value = "userId",required = false) String userId){
        ModelMap modelMap = new ModelMap();
        Map<String,Object> queryMap = new HashMap();
        queryMap.put("startTime", startTime);
        queryMap.put("endTime", endTime);
        queryMap.put("userId", userId);
        List<TbProjectTasktime> tasktimeList = projectTasktimeService.taskTimeStaffChange(queryMap);

        return  setSuccessModelMap(modelMap,tasktimeList);
    }

    @ApiOperation(value = "人天类型分布",notes = "员工人天统计")
    @RequestMapping(value = "/taskTimeTypeSpread2",method = RequestMethod.POST)
    public Object taskTimeTypeSpread2(
            @RequestParam(value = "startTime", required =  false) String startTime,
            @RequestParam(value = "endTime", required =  false) String endTime,
            @RequestParam(value = "userId",required = false) String userId){
        ModelMap modelMap = new ModelMap();
        Map<String,Object> queryMap = new HashMap();
        queryMap.put("startTime", startTime);
        queryMap.put("endTime", endTime);
        queryMap.put("userId", userId);
        List<TaskTimeTypeVo> taskTimeTypeVos = projectTasktimeService.taskTimeTypeSpread2(queryMap);
        List<Map<String,Object>> dataMap = new ArrayList<Map<String, Object>>();
        BigDecimal total = new BigDecimal(0);//总的人天
        MathContext mc = new MathContext(4, RoundingMode.HALF_DOWN);
        Set s = new HashSet();
        for(int i=0;i<taskTimeTypeVos.size();i++){
            s.add(taskTimeTypeVos.get(i).getTaskTimeTypeName());
        }

        for(TaskTimeTypeVo vo:taskTimeTypeVos){
            total = total.add(vo.getTaskTimeTotal());
        }
        Iterator<String> is = s.iterator();
        while(is.hasNext()){
            String typeName = is.next();
            Map map = new HashMap();
            List<TaskTimeTypeVo> voLis = new ArrayList<TaskTimeTypeVo>();
            BigDecimal totalPer = new BigDecimal(0);//每一阶段的人天
            for(TaskTimeTypeVo vo:taskTimeTypeVos){
                if(typeName.equals(vo.getTaskTimeTypeName())){

                    vo.setTaskRatio(vo.getTaskTimeTotal().divide(total,mc));
                    totalPer = totalPer.add(vo.getTaskTimeTotal());
                    voLis.add(vo);
                }
            }
            map.put("name",typeName);
            map.put("list",voLis);
            map.put("totalTaskTime",totalPer);
            dataMap.add(map);
        }
        return  setSuccessModelMap(modelMap,dataMap);

    }

    @ApiOperation(value = "项目人天分布",notes = "员工人天统计")
    @RequestMapping(value = "/taskTimeProjectSpread",method = RequestMethod.POST)
    public Object taskTimeProjectSpread(
            @RequestParam(value = "startTime", required =  false) String startTime,
            @RequestParam(value = "endTime", required =  false) String endTime,
            @RequestParam(value = "userId",required = false) String userId){

        ModelMap modelMap = new ModelMap();
        Map<String,Object> queryMap = new HashMap();
        queryMap.put("startTime", startTime);
        queryMap.put("endTime", endTime);
        queryMap.put("userId", userId);
        List<TaskTimeTypeVo> vos = projectTasktimeService.taskTimeProjectSpread(queryMap);
        List<Map<String,Object>> dataMap = new ArrayList<Map<String, Object>>();
        BigDecimal total = new BigDecimal(0);
        MathContext mc = new MathContext(4, RoundingMode.HALF_DOWN);
        Set set = new HashSet();
        for(TaskTimeTypeVo vo : vos){
            total = total.add(vo.getTaskTimeTotal());
            set.add(vo.getProjectName());
        }

        Iterator<String> is = set.iterator();
        while(is.hasNext()){
            String projectName = is.next();
            Map map = new HashMap();
            BigDecimal totalPer = new BigDecimal(0);
            List<TaskTimeTypeVo> list = new ArrayList<TaskTimeTypeVo>();
            for (TaskTimeTypeVo vo: vos){

                if(projectName.equals(vo.getProjectName())){
                    vo.setTaskRatio(vo.getTaskTimeTotal().divide(total,mc));
                    totalPer = totalPer.add(vo.getTaskTimeTotal());
                    list.add(vo);
                }
            }
            map.put("name",projectName);
            map.put("list",list);
            map.put("totalTaskTime",totalPer);
            dataMap.add(map);
            Collections.sort(dataMap, new Comparator<Map<String, Object>>() {
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    return((BigDecimal) o2.get("totalTaskTime")).compareTo((BigDecimal) o1.get("totalTaskTime"));
                }
            });
        }

        return setSuccessModelMap(modelMap,dataMap);
    }

}
